package cn.threewaters.qtyy.web.controller.sys;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.threewaters.qtyy.common.bean.PageBean;
import cn.threewaters.qtyy.common.bean.ResponseJson;
import cn.threewaters.qtyy.dit.one.entity.SysRole;
import cn.threewaters.qtyy.dit.one.entity.SysRoleFunction;
import cn.threewaters.qtyy.service.sys.SysFunctionService;
import cn.threewaters.qtyy.service.sys.SysRoleFunctionService;
import cn.threewaters.qtyy.service.sys.SysRoleService;
import cn.threewaters.qtyy.util.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "角色管理")
@Controller
@RequestMapping(value = "/sysRole")
public class SysRoleController {

	@Resource
	private SysRoleService sysRoleService;

	@Resource
	private SysFunctionService sysFunctionService;

	@Resource
	private SysRoleFunctionService sysRoleFunctionService;

	@ApiIgnore
	@ApiOperation(value = "角色管理主页面")
	@RequestMapping(value = "/main", method = RequestMethod.GET)
	public ModelAndView sysRoleMain() {
		return new ModelAndView("system/sys_role/sys_role_main");
	}

	@ApiIgnore
	@ApiOperation(value = "角色修改页面")
	@RequestMapping(value = "/update/view/{roleId}", method = RequestMethod.GET)
	public ModelAndView sysRoleUpdateView(@PathVariable(value = "roleId") String roleId, ModelMap modelMap)
			throws Exception {
		SysRole sysRole = sysRoleService.selectById(roleId);
		sysRole.setRoleFunctionJson(JSONUtil.toJsonStr(sysFunctionService.getRoleZtreeData(roleId)));
		modelMap.put("sysRole", sysRole);
		return new ModelAndView("system/sys_role/sys_role_update", modelMap);
	}

	@ApiIgnore
	@ApiOperation(value = "角色新增页面")
	@RequestMapping(value = "/add/view", method = RequestMethod.GET)
	public ModelAndView sysRoleAddView() {
		return new ModelAndView("system/sys_role/sys_role_add");
	}

	@ApiOperation(value = "分页查询角色集合")
	@ApiImplicitParams({ @ApiImplicitParam(name = "pageBean", value = "分页对象", required = true, dataType = "PageBean"),
			@ApiImplicitParam(name = "sysRole", value = "角色对象", required = true, dataType = "SysRole") })
	@RequestMapping(value = "/page/list", method = RequestMethod.GET)
	@ResponseBody
	public ResponseJson getSysRolePageList(PageBean pageBean, SysRole sysRole) throws Exception {
		Page<SysRole> sysRoles = sysRoleService.selectPage(
				new Page<SysRole>(pageBean.getPageNumber(), pageBean.getPageSize()),
				new EntityWrapper<SysRole>(sysRole));
		return ResponseJson.builder().success(true).obj(sysRoles).build();
	}

	@ApiOperation(value = "增加角色")
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson sysRoleAdd(@ApiParam SysRole sysRole) throws Exception {
		sysRoleService.insertSysRole(sysRole);
		return ResponseJson.builder().success(true).msg("保存成功").build();
	}

	@ApiOperation(value = "修改角色")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sysRole", value = "角色对象", required = true, dataType = "SysRole") })
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson sysRoleUpdate(SysRole sysRole) throws Exception {
		sysRoleService.updateSysRole(sysRole);
		return ResponseJson.builder().success(true).msg("更新成功").build();
	}

	@ApiOperation(value = "批量删除角色")
	@ApiImplicitParams({ @ApiImplicitParam(name = "idList", value = "角色ID数组", required = true, dataType = "Array") })
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public ResponseJson deleteSysRoles(@RequestBody List<String> idList) throws Exception {
		boolean isSuccess = sysRoleService.deleteBatchIds(idList);
		isSuccess = sysRoleFunctionService.delete(new EntityWrapper<SysRoleFunction>().in("role_id", idList));

		return ResponseJson.builder().success(isSuccess).msg("删除成功").build();
	}

}
